import request from "@/utils/request";
import fetchRequest from "@/utils/request/fetch";
import fetchRequestSimple from "@/utils/request/fetch";
import axios from "axios";

/** 对话列表数据 */
export interface TypeChatList {
  /** 页码 */
  page: string;
}
// 获取对话列表
export function chatList(params: TypeChatList) {
  return request({
    url: "/v1/chats/?page=1",
    method: "get",
    params,
  });
}
export function webSearch(stockCode: string, query: string = '') {
  const searchQuery = query ? `${stockCode} ${query}` : `${stockCode} 最新行情 新闻 分析`
  
  return axios.post('/web-search', {
    query: searchQuery,
    stockCode: stockCode
  })
}
export function changeUrl(params: { id: string; name: string; endpoint: string; apiUrl: string; secretKey: string; }) {
  return fetchRequest("/changeurl", {
    method: "POST",
    body: JSON.stringify(params)
  });
}

export async function getStockData(stockCode: any, startDate: any, endDate: any) {
  const params =
  {
    startDate: startDate,
    endDate: endDate,
    stockCode:stockCode
  }
  var response =  await axios.post("/getStockData", params);
  return response;
}

// 发送对话
// export function chatCompletions(stream: AbortSignal, messages: Chat.ConversationMessage[]) {
//   return request({
//     url: "/compatible-mode/v1/chat/completions",
//     method: "post",
//     signal: stream,
//     responseType: 'stream',
//     data: {
//       model: 'deepseek-r1',
//       messages: messages,
//       stream: true,
//       stream_options: {
//         include_usage: true
//       }
//     }
//   });
// }
export function chatCompletions(stream: AbortSignal, messages: Chat.ConversationMessage[]) {
  let apiUrl = ''
  let name = ''
  let temperature = 0.6
  let top_p = 0.95
  let max_tokens = 8192
  let repetition_penalty = 1.05
  let top_k = 30
  let min_p = 0
  const selectedModel = localStorage.getItem('selectedModel')
  if (selectedModel)
  {
        
    apiUrl = JSON.parse(selectedModel).apiUrl
    name = JSON.parse(selectedModel).name
    temperature = JSON.parse(selectedModel).temperature 
    top_p = JSON.parse(selectedModel).top_p
    max_tokens = JSON.parse(selectedModel).max_tokens
    repetition_penalty = JSON.parse(selectedModel).repetition_penalty
    top_k = JSON.parse(selectedModel).top_k
    min_p = JSON.parse(selectedModel).min_p
  }
  return fetchRequest(apiUrl, {
    method: "POST",
    body: JSON.stringify({
      model: name,
      temperature: temperature,
      top_p: top_p,
      top_k: top_k,
      min_p: min_p,
      max_tokens: max_tokens,
      messages: messages,
      extra_body: {
         "repetition_penalty": repetition_penalty
      },
      stream: true,
      stream_options: {
        include_usage: true,
      },
    }),
    signal: stream
  });
}
